# <link rel="stylesheet" type="text/css" href="../static/css/main.css">
# <script type="text/javascript" src="../static/js/host.js"></script>
# <script type="text/javascript" src="../static/js/common.js"></script>
# {{ static('css/reset.css') }}

# v1.1
import os
import re
import shutil

file_path = "../templates/"
file_list = os.listdir(file_path)
# 创建备份目录
if not os.path.exists(file_path+"backup/"):
    os.mkdir(file_path+"backup/")

for file_name in file_list:
    file = file_path + file_name
    if os.path.isdir(file):
        continue
    elif file[-5:] != ".html":
        continue

    # 备份
    shutil.copyfile(file, file_path + 'backup/' + file_name)

    with open(file) as f:
        content = f.read()
        while True:
            aa = re.search(r'"\.\./static/.+?[.js|.html|.css|.jpg|.png]"', content)
            if not aa:
                break
            start, end = aa.span()
            # print(content[start:end])
            static_str_orc =  content[start:end]
            static_str = static_str_orc.replace('"../static/','{{ static("')
            static_str_des = static_str[:-1] + '") }}'
            print(static_str_orc, static_str_des)
            content = content.replace(static_str_orc, static_str_des)

        with open(file,"w",encoding="utf-8") as f:
            f.write(content)